﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.NetworkCallbacks
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Collections.Generic;

#nullable disable
namespace Netick.Unity;

public class NetworkCallbacks
{
  private NetworkSandbox Sandbox;
  private List<NetworkEventsListener> _listeners = new List<NetworkEventsListener>(64 /*0x40*/);

  internal NetworkCallbacks(NetworkSandbox sandbox) => this.Sandbox = sandbox;

  public event OnConnectRequestDelegate OnConnectRequest;

  public event OnConnectFailedDelegate OnConnectFailed;

  public event OnStartupDelegate OnStartup;

  public event OnShutdownDelegate OnShutdown;

  public event OnVisibilityChangedDelegate OnVisibilityChanged;

  public event OnInputReadDelegate OnInput;

  public event OnDataReceivedDelegate OnDataReceived;

  public event OnSceneOperationDelegate OnSceneOperationBegan;

  public event OnSceneOperationDelegate OnSceneOperationDone;

  public event OnObjectCreatedDelegate OnObjectCreated;

  public event OnObjectCreatedDelegate OnObjectDestroyed;

  public event OnPlayerJoinedDelegate OnPlayerJoined;

  public event OnPlayerLeftDelegate OnPlayerLeft;

  public event OnConnectedToServerDelegate OnConnectedToServer;

  public event OnDisconnectedFromServerDelegate OnDisconnectedFromServer;

  public event OnMatchListUpdateDelegate OnMatchListUpdate;

  public event OnMatchCreatedDelegate OnMatchCreated;

  [Obsolete("This event will be removed. Use OnInput instead.")]
  public event OnInputReadDelegate OnInputRead;

  [Obsolete("This event will be removed. Use OnSceneLoadBegan instead.")]
  public event OnSceneLoadedDelegate OnSceneLoaded;

  [Obsolete("This event will be removed. Use OnSceneLoadDone instead.")]
  public event OnSceneLoadStartedDelegate OnSceneLoadStarted;

  [Obsolete("This method will be removed. Use OnPlayerJoined instead.")]
  public event OnClientConnectedDelegate OnClientConnected;

  [Obsolete("This method will be removed. Use OnPlayerLeft instead.")]
  public event OnClientDisconnectedDelegate OnClientDisconnected;

  [Obsolete("This method will be removed. Use OnPlayerJoined instead.")]
  public event OnPlayerConnectedDelegate OnPlayerConnected;

  [Obsolete("This method will be removed. Use OnPlayerLeft instead.")]
  public event OnPlayerDisconnectedDelegate OnPlayerDisconnected;

  internal unsafe void InvokeOnReceiveData(
    NetworkSandbox sandbox,
    NetworkConnection sender,
    byte id,
    byte* data,
    int len,
    TransportDeliveryMethod transportDeliveryMethod)
  {
    try
    {
      OnDataReceivedDelegate onDataReceived = this.OnDataReceived;
      if (onDataReceived == null)
        return;
      onDataReceived(sandbox, sender, id, data, len, transportDeliveryMethod);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnMatchListUpdate(NetworkSandbox sandbox, List<Session> sessions)
  {
    try
    {
      OnMatchListUpdateDelegate onMatchListUpdate = this.OnMatchListUpdate;
      if (onMatchListUpdate == null)
        return;
      onMatchListUpdate(sandbox, sessions);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnMatchCreated(NetworkSandbox sandbox)
  {
    try
    {
      OnMatchCreatedDelegate onMatchCreated = this.OnMatchCreated;
      if (onMatchCreated == null)
        return;
      onMatchCreated(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal bool InvokeOnConnectRequest(NetworkSandbox sandbox, NetworkConnectionRequest request)
  {
    try
    {
      OnConnectRequestDelegate onConnectRequest = this.OnConnectRequest;
      if (onConnectRequest != null)
        onConnectRequest(sandbox, request);
      return request.AllowConnection;
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
    return false;
  }

  internal void InvokeOnConnectFailed(NetworkSandbox sandbox, ConnectionFailedReason reason)
  {
    try
    {
      OnConnectFailedDelegate onConnectFailed = this.OnConnectFailed;
      if (onConnectFailed == null)
        return;
      onConnectFailed(sandbox, reason);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnStartup(NetworkSandbox sandbox)
  {
    try
    {
      OnStartupDelegate onStartup = this.OnStartup;
      if (onStartup == null)
        return;
      onStartup(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnShutdown(NetworkSandbox sandbox)
  {
    try
    {
      OnShutdownDelegate onShutdown = this.OnShutdown;
      if (onShutdown == null)
        return;
      onShutdown(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnVisibilityChanged(NetworkSandbox sandbox)
  {
    try
    {
      OnVisibilityChangedDelegate visibilityChanged = this.OnVisibilityChanged;
      if (visibilityChanged == null)
        return;
      visibilityChanged(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnInput(NetworkSandbox sandbox)
  {
    try
    {
      OnInputReadDelegate onInputRead = this.OnInputRead;
      if (onInputRead != null)
        onInputRead(sandbox);
      OnInputReadDelegate onInput = this.OnInput;
      if (onInput == null)
        return;
      onInput(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnSceneOperationBegan(
    NetworkSandbox sandbox,
    NetworkSceneOperation networkSceneOperation)
  {
    try
    {
      OnSceneOperationDelegate sceneOperationBegan = this.OnSceneOperationBegan;
      if (sceneOperationBegan == null)
        return;
      sceneOperationBegan(sandbox, networkSceneOperation);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnSceneOperationDone(
    NetworkSandbox sandbox,
    NetworkSceneOperation networkSceneOperation)
  {
    try
    {
      OnSceneOperationDelegate sceneOperationDone = this.OnSceneOperationDone;
      if (sceneOperationDone == null)
        return;
      sceneOperationDone(sandbox, networkSceneOperation);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnConnectedToServer(NetworkSandbox sandbox, NetworkConnection server)
  {
    try
    {
      OnConnectedToServerDelegate connectedToServer = this.OnConnectedToServer;
      if (connectedToServer == null)
        return;
      connectedToServer(sandbox, server);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnDisconnectedFromServer(
    NetworkSandbox sandbox,
    NetworkConnection server,
    TransportDisconnectReason transportDisconnectReason)
  {
    try
    {
      OnDisconnectedFromServerDelegate disconnectedFromServer = this.OnDisconnectedFromServer;
      if (disconnectedFromServer == null)
        return;
      disconnectedFromServer(sandbox, server, transportDisconnectReason);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnPlayerJoined(NetworkSandbox sandbox, NetworkPlayerId player)
  {
    try
    {
      OnPlayerJoinedDelegate onPlayerJoined = this.OnPlayerJoined;
      if (onPlayerJoined == null)
        return;
      onPlayerJoined(sandbox, player);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnPlayerLeft(NetworkSandbox sandbox, NetworkPlayerId player)
  {
    try
    {
      OnPlayerLeftDelegate onPlayerLeft = this.OnPlayerLeft;
      if (onPlayerLeft == null)
        return;
      onPlayerLeft(sandbox, player);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnObjectCreated(NetworkObject obj)
  {
    try
    {
      OnObjectCreatedDelegate onObjectCreated = this.OnObjectCreated;
      if (onObjectCreated == null)
        return;
      onObjectCreated(obj);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnObjectDestroyed(NetworkObject obj)
  {
    try
    {
      OnObjectCreatedDelegate onObjectDestroyed = this.OnObjectDestroyed;
      if (onObjectDestroyed == null)
        return;
      onObjectDestroyed(obj);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnClientConnected(NetworkSandbox sandbox, NetworkConnection client)
  {
    try
    {
      OnClientConnectedDelegate onClientConnected = this.OnClientConnected;
      if (onClientConnected == null)
        return;
      onClientConnected(sandbox, client);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnClientDisconnected(
    NetworkSandbox sandbox,
    NetworkConnection client,
    TransportDisconnectReason transportDisconnectReason)
  {
    try
    {
      OnClientDisconnectedDelegate clientDisconnected = this.OnClientDisconnected;
      if (clientDisconnected == null)
        return;
      clientDisconnected(sandbox, client, transportDisconnectReason);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnPlayerConnected(NetworkSandbox sandbox, NetworkPlayer player)
  {
    try
    {
      OnPlayerConnectedDelegate onPlayerConnected = this.OnPlayerConnected;
      if (onPlayerConnected == null)
        return;
      onPlayerConnected(sandbox, player);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnPlayerDisconnected(
    NetworkSandbox sandbox,
    NetworkPlayer player,
    TransportDisconnectReason transportDisconnectReason)
  {
    try
    {
      OnPlayerDisconnectedDelegate playerDisconnected = this.OnPlayerDisconnected;
      if (playerDisconnected == null)
        return;
      playerDisconnected(sandbox, player, transportDisconnectReason);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnSceneLoaded(NetworkSandbox sandbox)
  {
    try
    {
      OnSceneLoadedDelegate onSceneLoaded = this.OnSceneLoaded;
      if (onSceneLoaded == null)
        return;
      onSceneLoaded(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void InvokeOnSceneLoadStarted(NetworkSandbox sandbox)
  {
    try
    {
      OnSceneLoadStartedDelegate sceneLoadStarted = this.OnSceneLoadStarted;
      if (sceneLoadStarted == null)
        return;
      sceneLoadStarted(sandbox);
    }
    catch (Exception ex)
    {
      NetickLogger.LogError(this.Sandbox.Engine, (object) ex);
    }
  }

  internal void Subscribe(NetworkEventsListener eventListener, NetworkObject obj)
  {
    eventListener.Init(this.Sandbox, obj);
    if (this._listeners.Contains(eventListener))
      return;
    eventListener.Sandbox = this.Sandbox;
    this.OnDataReceived += new OnDataReceivedDelegate(eventListener.OnDataReceived);
    this.OnConnectRequest += new OnConnectRequestDelegate(eventListener.OnConnectRequest);
    this.OnConnectFailed += new OnConnectFailedDelegate(eventListener.OnConnectFailed);
    this.OnStartup += new OnStartupDelegate(eventListener.OnStartup);
    this.OnShutdown += new OnShutdownDelegate(eventListener.OnShutdown);
    this.OnVisibilityChanged += new OnVisibilityChangedDelegate(eventListener.OnVisibilityChanged);
    this.OnConnectedToServer += new OnConnectedToServerDelegate(eventListener.OnConnectedToServer);
    this.OnDisconnectedFromServer += new OnDisconnectedFromServerDelegate(eventListener.OnDisconnectedFromServer);
    this.OnPlayerJoined += new OnPlayerJoinedDelegate(eventListener.OnPlayerJoined);
    this.OnPlayerLeft += new OnPlayerLeftDelegate(eventListener.OnPlayerLeft);
    this.OnSceneOperationBegan += new OnSceneOperationDelegate(eventListener.OnSceneOperationBegan);
    this.OnSceneOperationDone += new OnSceneOperationDelegate(eventListener.OnSceneOperationDone);
    this.OnObjectCreated += new OnObjectCreatedDelegate(eventListener.OnObjectCreated);
    this.OnObjectDestroyed += new OnObjectCreatedDelegate(eventListener.OnObjectDestroyed);
    this.OnMatchListUpdate += new OnMatchListUpdateDelegate(eventListener.OnMatchListUpdate);
    this.OnMatchCreated += new OnMatchCreatedDelegate(eventListener.OnMatchCreated);
    this.OnInputRead += new OnInputReadDelegate(eventListener.OnInput);
    this.OnClientConnected += new OnClientConnectedDelegate(eventListener.OnClientConnected);
    this.OnClientDisconnected += new OnClientDisconnectedDelegate(eventListener.OnClientDisconnected);
    this.OnPlayerConnected += new OnPlayerConnectedDelegate(eventListener.OnPlayerConnected);
    this.OnPlayerDisconnected += new OnPlayerDisconnectedDelegate(eventListener.OnPlayerDisconnected);
    this.OnSceneLoaded += new OnSceneLoadedDelegate(eventListener.OnSceneLoaded);
    this.OnSceneLoadStarted += new OnSceneLoadStartedDelegate(eventListener.OnSceneLoadStarted);
    this._listeners.Add(eventListener);
  }

  internal void Unsubscribe(NetworkEventsListener eventListener)
  {
    if (!this._listeners.Contains(eventListener))
      return;
    this.OnDataReceived -= new OnDataReceivedDelegate(eventListener.OnDataReceived);
    this.OnConnectRequest -= new OnConnectRequestDelegate(eventListener.OnConnectRequest);
    this.OnConnectFailed -= new OnConnectFailedDelegate(eventListener.OnConnectFailed);
    this.OnStartup -= new OnStartupDelegate(eventListener.OnStartup);
    this.OnShutdown -= new OnShutdownDelegate(eventListener.OnShutdown);
    this.OnVisibilityChanged -= new OnVisibilityChangedDelegate(eventListener.OnVisibilityChanged);
    this.OnConnectedToServer -= new OnConnectedToServerDelegate(eventListener.OnConnectedToServer);
    this.OnDisconnectedFromServer -= new OnDisconnectedFromServerDelegate(eventListener.OnDisconnectedFromServer);
    this.OnPlayerJoined -= new OnPlayerJoinedDelegate(eventListener.OnPlayerJoined);
    this.OnPlayerLeft -= new OnPlayerLeftDelegate(eventListener.OnPlayerLeft);
    this.OnSceneOperationBegan -= new OnSceneOperationDelegate(eventListener.OnSceneOperationBegan);
    this.OnSceneOperationDone -= new OnSceneOperationDelegate(eventListener.OnSceneOperationDone);
    this.OnObjectCreated -= new OnObjectCreatedDelegate(eventListener.OnObjectCreated);
    this.OnObjectDestroyed -= new OnObjectCreatedDelegate(eventListener.OnObjectDestroyed);
    this.OnMatchListUpdate -= new OnMatchListUpdateDelegate(eventListener.OnMatchListUpdate);
    this.OnMatchCreated -= new OnMatchCreatedDelegate(eventListener.OnMatchCreated);
    this.OnInputRead -= new OnInputReadDelegate(eventListener.OnInput);
    this.OnClientConnected -= new OnClientConnectedDelegate(eventListener.OnClientConnected);
    this.OnClientDisconnected -= new OnClientDisconnectedDelegate(eventListener.OnClientDisconnected);
    this.OnPlayerConnected -= new OnPlayerConnectedDelegate(eventListener.OnPlayerConnected);
    this.OnPlayerDisconnected -= new OnPlayerDisconnectedDelegate(eventListener.OnPlayerDisconnected);
    this.OnSceneLoaded -= new OnSceneLoadedDelegate(eventListener.OnSceneLoaded);
    this.OnSceneLoadStarted -= new OnSceneLoadStartedDelegate(eventListener.OnSceneLoadStarted);
    this._listeners.Remove(eventListener);
    eventListener.Sandbox = (NetworkSandbox) null;
  }
}
